package com.code.view.preference;

import java.io.IOException;

import org.eclipse.jface.preference.DirectoryFieldEditor;
import org.eclipse.jface.preference.PreferencePage;
import org.eclipse.jface.preference.PreferenceStore;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;

import com.code.plugin.LabelProperties;
import com.code.view.ViewConstant;
import com.code.view.ViewUtils;
import com.code.view.entity.PreferenceVO;


/**
 * 首选项页面
 *
 *
 * @author 朱国军
 * @version 2015年2月28日 朱国军
 */
public class CodeGeneratorPage extends PreferencePage implements IWorkbenchPreferencePage {

    private Text userName;
    private Text password;
    private Text dbUrl;
    private Combo dbDriver;
    private Text author;
    private Text entitySuffix;
    private Text commonPackage;
    private Text packagePath;

    private DirectoryFieldEditor outPutPath;


    @Override
    public void init(IWorkbench workbench) {

    }


    @Override
    protected Control createContents(Composite parent) {
        Composite composite = new Composite(parent, SWT.NONE);

        composite.setLayout(new GridLayout(3, false));

        GridData textStyle = new GridData(GridData.FILL, GridData.CENTER, true, false, 2, 1);

        new Label(composite, SWT.LEFT).setText(LabelProperties.CodeGenerator_dbUserName); //$NON-NLS-1$
        userName = new Text(composite, SWT.BORDER);
        userName.setLayoutData(textStyle);

        new Label(composite, SWT.LEFT).setText(LabelProperties.CodeGenerator_dbPassword); //$NON-NLS-1$
        password = new Text(composite, SWT.BORDER);
        password.setLayoutData(textStyle);

        new Label(composite, SWT.LEFT).setText(LabelProperties.CodeGenerator_dbUrl); //$NON-NLS-1$
        dbUrl = new Text(composite, SWT.BORDER);
        dbUrl.setLayoutData(textStyle);

        new Label(composite, SWT.LEFT).setText(LabelProperties.CodeGenerator_dbDriver); //$NON-NLS-1$
        dbDriver = new Combo(composite, SWT.READ_ONLY);
        dbDriver.setItems(ViewConstant.DB_DRIVERS);
        dbDriver.setLayoutData(textStyle);

        new Label(composite, SWT.LEFT).setText(LabelProperties.CodeGenerator_author); //$NON-NLS-1$
        author = new Text(composite, SWT.BORDER);
        author.setLayoutData(textStyle);

        Label entitySuffixLabel = new Label(composite, SWT.LEFT);
        entitySuffixLabel.setText(LabelProperties.CodeGenerator_entitySuffix); //$NON-NLS-1$
        entitySuffix = new Text(composite, SWT.BORDER);
        entitySuffix.setLayoutData(textStyle);

        Label commonPackageLabel = new Label(composite, SWT.LEFT);
        commonPackageLabel.setText(LabelProperties.CodeGenerator_commonPackage); //$NON-NLS-1$
        commonPackage = new Text(composite, SWT.BORDER);
        commonPackage.setLayoutData(textStyle);

        new Label(composite, SWT.LEFT).setText(LabelProperties.CodeGeneratorPage_packagePath); //$NON-NLS-1$
        packagePath = new Text(composite, SWT.BORDER);
        packagePath.setLayoutData(textStyle);

        outPutPath = new DirectoryFieldEditor("outPutPath", LabelProperties.CodeGenerator_outPutPath, composite); //$NON-NLS-1$ //$NON-NLS-2$

        readPropertyValue();

        return composite;
    }


    @Override
    protected void performDefaults() {
        setDefaultsValues();
        super.performDefaults();
    }


    @Override
    public void performApply() {
        savePropertyValue();
    }


    @Override
    public boolean performOk() {
        savePropertyValue();
        return super.performOk();
    }


    private void readPropertyValue() {
        PreferenceVO preferenceVO = ViewUtils.readPreferencePropertys();
        userName.setText(preferenceVO.getDbUserName());
        password.setText(preferenceVO.getDbPassword());
        dbUrl.setText(preferenceVO.getDbUrl());
        dbDriver.setText(preferenceVO.getDbDriver());
        author.setText(preferenceVO.getAuthor());
        packagePath.setText(preferenceVO.getPackagePath());
        outPutPath.setStringValue(preferenceVO.getOutPutPath());
        entitySuffix.setText(preferenceVO.getEntitySuffix());
        commonPackage.setText(preferenceVO.getCommonPackage());
    }


    private void setDefaultsValues() {
        PreferenceVO preferenceVO = ViewUtils.getDefaultsPreference();
        userName.setText(preferenceVO.getDbUserName());
        password.setText(preferenceVO.getDbPassword());
        dbUrl.setText(preferenceVO.getDbUrl());
        dbDriver.setText(preferenceVO.getDbDriver());
        author.setText(preferenceVO.getAuthor());
        packagePath.setText(preferenceVO.getPackagePath());
        outPutPath.setStringValue(preferenceVO.getOutPutPath());
        entitySuffix.setText(preferenceVO.getEntitySuffix());
        commonPackage.setText(preferenceVO.getCommonPackage());
        savePropertyValue();
    }


    private void savePropertyValue() {
        String propertyFilePath = ViewUtils.getPreferenceStoreFile();
        // 先判断文件夹是否存在，不存在就创建文件夹
        ViewUtils.createFolder(propertyFilePath, true);
        PreferenceStore preferenceStore = new PreferenceStore(propertyFilePath);
        preferenceStore.setValue(ViewConstant.FIELD_NAME_USERNAME, userName.getText());
        preferenceStore.setValue(ViewConstant.FIELD_NAME_PASSWORD, password.getText());
        preferenceStore.setValue(ViewConstant.FIELD_NAME_DBURL, dbUrl.getText());
        preferenceStore.setValue(ViewConstant.FIELD_NAME_DBDRIVER, dbDriver.getText());
        preferenceStore.setValue(ViewConstant.FIELD_NAME_AUTHOR, author.getText());
        preferenceStore.setValue(ViewConstant.FIELD_NAME_PACKAGEPATH, packagePath.getText());
        preferenceStore.setValue(ViewConstant.FIELD_NAME_OUTPUTPATH, outPutPath.getStringValue());
        preferenceStore.setValue(ViewConstant.FIELD_NAME_ENTITY_SUFFIX, entitySuffix.getText());
        preferenceStore.setValue(ViewConstant.FIELD_NAME_COMMON_PACKAGE, commonPackage.getText());
        try {
            preferenceStore.save();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
